//
//  UIColor+Custom.m
//  coyote
//
//  Created by LeoHao on 14-8-19.
//  Copyright (c) 2014年 500wan. All rights reserved.
//

#import "UIColor+Custom.h"

@implementation UIColor (Custom)

/**
 @desc      获取纯色图片
 @param    frame 图片大小
 @return    纯色图片
 @authorCreated by LeoHao on 2014-08-19
 */
-(UIImage*)getPureImage:(CGRect)frame {
    UIGraphicsBeginImageContext(frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, self.CGColor);
    CGContextFillRect(context, frame);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    context = nil;
    return image;
}

/**
 @desc      获取圆角纯色图片
 @param    frame  图片大小
 @param    CornerRadius 圆角大小
 @return    圆角纯色图片
 @authorCreated by LeoHao on 2014-08-19
 */
-(UIImage*)getPureImage:(CGRect)frame cornerRadius:(CGFloat)CornerRadius {
    UIGraphicsBeginImageContext(frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, self.CGColor);
    CGContextFillRect(context, frame);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    context = nil;
    
    UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale);
    const CGRect RECT = CGRectMake(0, 0, image.size.width, image.size.height);
    [[UIBezierPath bezierPathWithRoundedRect:RECT cornerRadius:CornerRadius] addClip];
    [image drawInRect:RECT];
    UIImage* roundedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return roundedImage;
}

/**
 @desc      获取纯色图片
 @return    纯色图片
 @authorCreated by LeoHao on 2014-08-19
 */
-(UIImage*)getPureImage{
    CGRect rect_frame = CGRectMake(0, 0, 50, 30);
    
    UIGraphicsBeginImageContext(rect_frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, self.CGColor);
    CGContextFillRect(context, rect_frame);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    context = nil;
    return image;
}

/**
@desc 返回随机颜色
 */
+ (instancetype)rand
{
    return [self colorWithRed:rand()%256/255.0
                        green:rand()%256/255.0
                         blue:rand()%256/255.0
                        alpha:1];
}


/**
 @desc      通过16进制的颜色（字符串标识的），获取对应的color值
 @author    Created by LeoHao on 2015-5-6
 */
+ (UIColor *)colorWithHexString:(NSString *)stringToConvert
{
    return [self colorWithHexString:stringToConvert alpha:1.0];
}

+ (UIColor *)colorWithHexString:(NSString *)stringToConvert alpha:(CGFloat) alpha{
    NSString *cString = [[stringToConvert stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    
    // String should be 6 or 8 characters
    if ([cString length] < 6){
        return [UIColor darkTextColor];
    }
    
    // strip 0X if it appears
    if ([cString hasPrefix:@"0X"]){
        cString = [cString substringFromIndex:2];
    }
    if ([cString hasPrefix:@"#"]) {
        cString = [cString substringFromIndex:1];
    }
    if ([cString length] != 6){
        return [UIColor darkTextColor];
    }
    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length = 2;
    NSString *rString = [cString substringWithRange:range];
    
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    
    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    
    return [UIColor colorWithRed:((float) r / 255.0f)
                           green:((float) g / 255.0f)
                            blue:((float) b / 255.0f)
                           alpha:alpha];
}
@end
